home *** CD-ROM | disk | FTP | other *** search
- Path: lerc.nasa.gov!purdue!yuma!steffend
- From: steffend@lamar.colostate.edu (Dave Steffen)
- Newsgroups: comp.lang.c++
- Subject: Re: Yet a question about random
- Date: 27 Feb 1996 17:55:48 GMT
- Organization: Colorado State University, Fort Collins, CO 80523
- Distribution: world
- Message-ID: <4gvgj4$4avq@yuma.ACNS.ColoState.EDU>
- References: <4gplos$lds@cc.tut.fi>
- NNTP-Posting-Host: glitch.physics.colostate.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Edvard Majakari (m151843@proffa.cc.tut.fi) wrote:
- > I use the following function to produce (pseudo)random numbers
- > between 1...top. However, this algorithm seems to be far from
- > good pseudorandom. But the hint was in rand manpage - any good
- > ideas to produce better 'random' numbers?
-
- > -----&<------------------------&<-------------------------------
-
- > #include <stdlib.h>
- > #include <sys/time.h>
- > #include <unistd.h>
-
- > int randomize(float top) {
- > int rnum;
- > timeval seed; //see struct timeval for further details
- > gettimeofday(&seed, NULL);
- > srand(seed.tv_usec); //initialize with millisecs
- > rnum = 1+(int) (top*rand()/(RAND_MAX+1.0));
- > return rnum;
- > }
-
- > -----&<------------------------&<-------------------------------
- > --
- > //Ed http://lodge.ton.tut.fi/~ed/
-
-
- Check out "Numerical Recipes in C" 2nd Ed for several better
- random number generators; there are lots of other good references but
- I don't know about them personally. In general, the "system-supplied"
- random generator should not be trusted.
- /\
- \/
-
- Dave Steffen No, his mind is not for rent
- Dept. of Physics To any God or Government
- Colorado State University Always hopeful, yet discontent
- steffend@lamar.colostate edu He knows changes aren't permanent-
- But change is...
- "Speak softly...
- ... and carry a black belt!" -Neal Peart / RUSH
- -----------------------------------------------------------------------
-